#include "qformlistwidget.h"

#include "../../lib/shared/qproject.h"
#include "../../include/abstractcore.h"

#include "widgethost.h"
#include "qpropertylistview.h"
#include "qformwidget.h"
#include "qpagewidget.h"

#include <QVBoxLayout>
#include <QMessageBox>

QFormListWidget::QFormListWidget(AbstractCore *core, QPropertyListView *listView, QWidget *parent):
    QWidget(parent),
    m_listview(listView),
    m_core(core)
{
    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    this->setLayout(l);

    connect(QProject::project(),SIGNAL(openProject()),this,SLOT(projectOpen()));
    connect(QProject::project(),SIGNAL(beforeClosed()),this,SLOT(projectClosed()));
    connect(QProject::project(),SIGNAL(changed(int,int)),this,SLOT(projectChanged(int,int)));
    connect(QProject::project(),SIGNAL(saveProject()),this,SLOT(projectSave()));
}

void QFormListWidget::addForm(const QString &name, const QString &data,int index)
{
    if(m_nameToForm.keys().indexOf(name)>=0)
    {
        return;
    }
    WidgetHost *host=new WidgetHost(this);
    QFormWidget *form=new QFormWidget(m_core,this);

    form->setWidgetHost(host);
    form->setName(name);
    form->setListView(m_listview);
    connect(form,SIGNAL(changed()),this,SLOT(changed()));
    m_nameToForm.insert(name,host);
    QPageWidget::pageWidget()->insertPage(name,form);
    if(data!="")
    {
        form->load(data);
    }
    host->setFormWidget(form);

    if(index<0 || index>=m_formlist.size())
    {
        index=m_formlist.size();
    }
    layout()->addWidget(host);
    host->setVisible(false);
    form->setDirty(false);
    form->setProperty("pageName",name);
    m_formlist.insert(index,host);
}

void QFormListWidget::showForm(const QString &name)
{
    WidgetHost *host=m_nameToForm.value(name);
    WidgetHost *current=m_nameToForm.value(m_current);
    m_current=name;
    if(current!=NULL)
    {
        current->setVisible(false);
    }
    if(host!=NULL)
    {

        host->setVisible(true);
        host->formWidget()->selectThis();
        host->formWidget()->setFocus();
    }
}

void QFormListWidget::projectOpen()
{
    QProject *p=QProject::project();
    int count=p->getPageCount();

    tagPageInfo info;

    for(int i=0;i<count;i++)
    {
        info=p->getPage(i);
        addForm(info.m_name,info.m_data);
    }
    if(count>0)
    {
        showForm(p->getPage(0).m_name);
    }
}

void QFormListWidget::changed()
{
    QFormWidget * form=(QFormWidget*)sender();
    int index=m_formlist.indexOf(form->widgetHost());

    QProject *p=QProject::project();
    if(index<0 || index>=p->getPageCount())
    {
        return;
    }
    tagPageInfo info;
    info.m_data=form->saveBuffer();
    info.m_name=form->name();

    p->setPage(index,info);
    emit updateFormName(index);
}

void QFormListWidget::projectClosed()
{
    qDeleteAll(m_formlist);
    m_formlist.clear();
    m_nameToForm.clear();
    QPageWidget::releasePageWidget();
}

bool QFormListWidget::dirty(int index)
{
    return m_formlist.at(index)->formWidget()->dirty();
}

void QFormListWidget::projectChanged(int type, int index)
{
    QProject *p=QProject::project();
    if(type==PCT_PAGE_DATA_ADD)
    {
        tagPageInfo info=p->getPage(index);
        addForm(info.m_name,info.m_data,index);
    }
    else if(type==PCT_PAGE_DATA_DEL)
    {
        tagPageInfo info=p->getPage(index);
        WidgetHost *host=m_nameToForm.value(info.m_name);
        if(host!=NULL)
        {

            m_nameToForm.remove(info.m_name);
            m_formlist.removeAll(host);
            delete host;
        }
    }
}

void QFormListWidget::projectSave()
{
    foreach(WidgetHost *host,m_formlist)
    {
        host->formWidget()->setDirty(false);
    }
}

void QFormListWidget::alignLeft()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignLeft();
}

void QFormListWidget::alignRight()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignRight();
}

void QFormListWidget::alignTop()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignTop();
}

void QFormListWidget::alignBottom()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignBottom();
}


void QFormListWidget::alignVCenter()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignVCenter();
}

void QFormListWidget::alignHCenter()
{
    WidgetHost *current=m_nameToForm.value(m_current);
    if(current==NULL)
    {
        return;
    }

    current->formWidget()->alignHCenter();
}

QFormWidget *QFormListWidget::currentForm()
{
    WidgetHost *host=m_nameToForm.value(m_current,NULL);

    return host==NULL?NULL:host->formWidget();
}
